﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Scenario.WorkflowEngine;
using Scenario.WAPI.Exception;
using Scenario.WorkflowEngine.DataModel.WorkflowProcesses;

namespace Criteria_Engine_Console
{
    public partial class Form1 : Form
    {
        private enum EngineState
        {
            STARTED,
            STOPPED
        }

        private EngineState state = EngineState.STOPPED;

        public Form1()
        {
            InitializeComponent();

            this.manageButton();
        }

        private void btnStartUp_Click(object sender, EventArgs e)
        {
            try
            {
                //Engine.Instance.StartUp(this.txtConfiguationFile.Text, false);
                Engine.Instance.StartUp(String.Empty, false);
                state = EngineState.STARTED;
                this.manageButton();

                //retrieve Workflow Template
                XMLWorkflows xmlWorkflows = null;
                xmlWorkflows = Engine.ProcessHelper.GetWorkflowsTemplate();
                for (int i = 0; i < xmlWorkflows.Count; i++)
                {
                    XMLWorkflow xmlWorkflow = (XMLWorkflow)xmlWorkflows[i];
                    int posDot = xmlWorkflow.Name.IndexOf(".");
                    this.ddlProcessTemplate.Items.Add(xmlWorkflow.Name.Substring(0, posDot));
                }
            }
            catch (EngineException err)
            {
                this.lblLogs.Text = err.Message;
            }
        }

        private void btnShutDown_Click(object sender, EventArgs e)
        {
            try
            {
                Engine.Instance.ShutDown();
                state = EngineState.STOPPED;
                this.manageButton();
            }
            catch (EngineException err)
            {
                this.lblLogs.Text = err.Message;
            }
        }

        private void manageButton() 
        {
            switch (state)
            {
                case EngineState.STARTED:
                    this.btnShutDown.Enabled = true;
                    this.btnStartUp.Enabled = false;
                    this.btnStartProcess.Enabled = true;
                    ddlProcessTemplate.Enabled = true;
                    this.btnLoadAttributes.Enabled = true;
                    break;

                case EngineState.STOPPED:
                    this.btnShutDown.Enabled = false;
                    this.btnStartUp.Enabled = true;
                    this.btnStartProcess.Enabled = false;
                    ddlProcessTemplate.Enabled = false;
                    this.btnLoadAttributes.Enabled = false;
                    this.lstDataTypes.Items.Clear();
                    break;
            }
        }

        private void btnStartProcess_Click(object sender, EventArgs e)
        {
            try
            {
                Engine.ProcessHelper.StartWorkflow(this.ddlProcessTemplate.Text, null, !(chkAsynch.Checked));
                this.lblLogs.Text = "Process Started correctly.";
            }
            catch (EngineException err)
            {
                this.lblLogs.Text = err.Message;
            }
            
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog.ShowDialog();

            this.txtConfiguationFile.Text = openFileDialog.FileName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Scenario.WorkflowEngine.DataModel.WorkflowDataTypes.XMLDataTypes types = null;
            types = Engine.DataTypesHelper.GetDataTypes();

            foreach (Scenario.WorkflowEngine.DataModel.WorkflowDataTypes.XMLDataType xmlType in types)
            {
                lstDataTypes.Items.Add(xmlType.Key);
            }
        }
    }
}
